
package ru.spbu.math.android.cbrf.view;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Spinner;
import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.dataprovider.DataProvider;
import ru.spbu.math.android.cbrf.model.RateType;

/**
 * Created by IntelliJ IDEA.
 * User: sandman
 * Date: 19.03.2010
 * Time: 10:17:05
 * To configure what kind of data
 * should take a widget place
 */

public class ItemsConfigurationActivity extends Activity {

    private String LOG_TAG = "ItemsConfigurationActivity";
    private int wID;        //  the ID of widget that starts this Activity

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        wID = getIntent().getFlags();
        Log.d(LOG_TAG, "Choose content for a widget with Id : " + wID);

        setContentView(R.layout.list_configuration_activity);
        LinearLayout l = (LinearLayout) findViewById(R.id.ListConf);
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        DataProvider dataProvider = DataProvider.getInstance(this);

        for (DataProvider.RateTypeGroups rateTypesGroup : DataProvider.RateTypeGroups.values()) {
            Spinner s = new Spinner(this);

            RateTypesListAdapter adapter = new RateTypesListAdapter(this,
                    dataProvider.getRateTypesByGroup(rateTypesGroup));
            s.setAdapter(adapter);
            s.setPrompt(getBaseContext().getResources().getString(R.string.Select) + " " + changeName(rateTypesGroup) + " :");
            s.setOnItemSelectedListener(new OnRateTypeClickListener(setName(rateTypesGroup)));

            l.addView(s, p);
        }
    }
    
    private String changeName(DataProvider.RateTypeGroups rate){
    	
    	if(rate.equals(DataProvider.RateTypeGroups.Metal)){
    		return getBaseContext().getResources().getString(R.string.Metal_mod);
    	}
    	if(rate.equals(DataProvider.RateTypeGroups.Currency)){
    		return getBaseContext().getResources().getString(R.string.Currency_mod);
    	}
    	if(rate.equals(DataProvider.RateTypeGroups.Oil)){
    		return getBaseContext().getResources().getString(R.string.Oil_mod);
    	}
    	return null;
    }
    
    private String setName(DataProvider.RateTypeGroups rate){
    	if(rate.equals(DataProvider.RateTypeGroups.Metal)){
    		return getBaseContext().getResources().getString(R.string.Metal);
    	}
    	if(rate.equals(DataProvider.RateTypeGroups.Currency)){
    		return getBaseContext().getResources().getString(R.string.Currency);
    	}
    	if(rate.equals(DataProvider.RateTypeGroups.Oil)){
    		return getBaseContext().getResources().getString(R.string.Oil);
    	}
    	return null;
    }

    @Override
    protected void onDestroy() {
        RateType selectedItem = OnRateTypeClickListener.getLastSelectedRateType();
        if (selectedItem != null) {
            Log.d(LOG_TAG, "Selected item: " + selectedItem);
            UsdCourseWidget.registerWidget(this, wID, selectedItem);
        }
        OnRateTypeClickListener.deactivate();
        RateTypesListAdapter.deactivate();
        super.onDestroy();
    }
}

